package com.F02.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.F02.action.F020206Action;
import com.F02.actionform.F020205ActionForm;
import com.F02.actionform.F020206ActionForm;
import com.F02.dao.F020205DAO;
import com.F02.dao.F020206DAO;
import com.action.BaseAction;
import com.config.Constants;
import com.dao.common.DbOperationExt;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.SessionUtils;

/**
 * @leip
 * @标准产品明细查询
 * @功能：初始化，查询
 * @规范：execute()方法制作判断转发，不处理具体业务�? @ ACTION中所有�?�辑判断对应的具体业务方法的命名规则
 * do+静态参数，例如:operationType=init，实现方法为doINIT(). @ 涉及到连接数据库的方法全部写在DAO中�
 * @操作记录：对于insert，update，delete操作系统�?要记录�??
 * @调试信息打印使用LOG.INFO();
 * @记得加注释

 */

public class F020206Action extends BaseAction{
	/** 记录操作日志 */
	Logger log = Logger.getLogger(F020206Action.class);

	/** 操作记录INFO */
	T_S_YHCZXXinfo t_YhczxxInfo = null;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		/** 获得actionForm对象 */
		F020206ActionForm actionForm = (F020206ActionForm) form;
		/** 获得operationType值�?�做执行判断�? */
		String operationType = actionForm.getOperationType();
		log.info("operationType = " + operationType);
		/** 获取实例 */
		F020206DAO dao = new F020206DAO();
		/**页面语言选择提示*/
		String language = SessionUtils.getLocale(request);
		actionForm.setLanguage(language);
		try {
			/** 生成操作记录语句 */
			t_YhczxxInfo = setYhczxx(request, response, operationType,
					"F020105");
			/**
			 * @页面初始�?
			 * @统一参数：operationType=init
			 * @规范：左树进入连接页面不执行查询�?
			 */
			if (Constants.INIT.equals(operationType)) {
				this.doINIT(actionForm,dao);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @查询功能
			 * @统一参数：operationType=queryList
			 * @规范：查询后回显查询条件。如果查询结果无记录，在页面给出提示�?
			 */
			if (Constants.QUERYLIST.equals(operationType)) {
				this.doQUERYLIST(actionForm, dao, request);
				return mapping.findForward(Constants.List);
			}	
			/**
			 * @导出功能
			 * @统一参数：operationType=export
			 * @规范：查询后回显查询条件。如果查询结果无记录，在页面给出提示�?
			 */
			if("export".equals(operationType)){
				doExport(actionForm, dao,request,response);
				return mapping.findForward("list");
			}
			/**
			 * @返回List页面
			 * @统一参数：operationType=back
			 * @规范：返回LIST页面，显示查询结果

			 */
			if (Constants.BACK.equals(operationType)) {
				this.doActionForm(actionForm, dao, request);
				return mapping.findForward(Constants.List);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null)
				dao.disConnect();
		}
		return mapping.findForward(Constants.INIT);
	}

	/**
	 * @初始�?
	 */
	public void doINIT(F020206ActionForm actionForm,F020206DAO dao) throws SelectException {		
		actionForm.setCurrentPage(1);
	}

	/**
	 * @查询
	 */
	private void doQUERYLIST(F020206ActionForm actionForm, F020206DAO dao,
			HttpServletRequest request) throws SelectException {
		List list = dao.QUERYLIST(actionForm, request);
		if(list!=null&&list.size()>0){
			actionForm.setList(list);
		}else{
			actionForm.setList(list);
			actionForm.setErrorFlag("2");
		}
	}	

	/**
	 * @throws SelectException
	 * @清空actionForm
	 * @执行查询回显操作
	 */
	private void doActionForm(F020206ActionForm actionForm, F020206DAO dao,
			HttpServletRequest request) throws SelectException {
		actionForm.setId("");		
		actionForm.setOperationType("");
		this.doQUERYLIST(actionForm, dao, request);
	}
	/**
	 * @导出
	 */
	private void doExport(F020206ActionForm actionForm, F020206DAO dao, 
			HttpServletRequest request,HttpServletResponse response){
		List titleList = new ArrayList();
		List totalList = new ArrayList();
		List headList = new ArrayList();
		try {			
			String fileName = request.getParameter("title");
			/* 标题设定 */
			titleList.add(request.getParameter("pname"));
			titleList.add(request.getParameter("mcode"));
			titleList.add(request.getParameter("mname"));
			titleList.add(request.getParameter("mdecp"));
			titleList.add(request.getParameter("qty"));
			titleList.add(request.getParameter("meas"));
			titleList.add(request.getParameter("price"));
			titleList.add(request.getParameter("tprice"));
			/* 拼装导出主体信息 */
			headList = dao.getNotStandHead(actionForm,titleList,request);			
			/* 拼装导出明细信息 */
			String sql = dao.getNotStandBody(actionForm);
			/* 拼装导出总价信息 */
			totalList = dao.getNotStandFeet(actionForm,request);
			/* excel输出 */
			dao.exportProductBOM(fileName, actionForm.getProductCode(), headList, sql, totalList, actionForm, response);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null)
				dao.disConnect();
		}
	}	
}
